/**
 * BCFixedScreen
 * 屏幕适配，支持横竖屏切换
 */

import { _decorator, Component, Node, Canvas, Size, view, UITransform, ResolutionPolicy, v3, size, screen, Enum } from 'cc';
import { BCPlatformMgr } from '../../controller/platform/BCPlatformMgr';
import { BCProjectDefines } from '../../utils/BCProjectDefines';
const { ccclass, property } = _decorator;

@ccclass('BCFixedScreen')
export class BCFixedScreen extends Component {
	@property(Node)
	private m_centerView: Node = null;

	@property({ type: Enum(BCProjectDefines.ScreenOrientation)})
	private m_screenOrientation: BCProjectDefines.ScreenOrientation = BCProjectDefines.ScreenOrientation.Landscape;
	set screenOrientation(orientation: BCProjectDefines.ScreenOrientation){
		this.m_screenOrientation = orientation;
		const canvasSize: Size = this._fixedCanvas();
		this._fixedBg(canvasSize);
	}

	get _screenOrientation(): BCProjectDefines.ScreenOrientation{
		return this.m_screenOrientation;
	}

	onLoad() {
		const canvasSize: Size = this._fixedCanvas();
		this._fixedBg(canvasSize);
	}

	private _fixedCanvas(): Size {
		const canvas = this.node.getComponent(Canvas);
		if (!canvas) {
			return;
		}

		
		let designSize: Size = null;
		const screenWidth = screen.windowSize.width;
		const screenHeight = screen.windowSize.height;
		let finalWidth: number = 0, finalHeight: number = 0;
		if (this.m_screenOrientation == BCProjectDefines.ScreenOrientation.Portrait){
			BCPlatformMgr.instance.setScreenOrientation(BCProjectDefines.ScreenOrientation.Portrait);
			designSize = BCProjectDefines.PortraitViewDesignSize;
			if (screenWidth > screenHeight){
				finalWidth = screenHeight;
				finalHeight = screenWidth;
				screen.windowSize = new Size(screenHeight, screenWidth);
			} else {
				finalWidth = screenWidth;
				finalHeight = screenHeight;
				screen.windowSize = new Size(screenWidth, screenHeight);
			}	
		} else {
			BCPlatformMgr.instance.setScreenOrientation(BCProjectDefines.ScreenOrientation.Landscape);
			designSize = BCProjectDefines.LandscapeViewDesignSize;
			if (screenWidth > screenHeight) {
				finalWidth = screenWidth;
				finalHeight = screenHeight;
				screen.windowSize = new Size(screenWidth, screenHeight);
			} else {
				finalWidth = screenHeight;
				finalHeight = screenWidth;
				screen.windowSize = new Size(screenHeight, screenWidth);
			}
		}
		const frameWidth: number = finalWidth, frameHeight: number = finalHeight;
		
		let centerScale = 1;
		if ((frameWidth / frameHeight) > (designSize.width / designSize.height)) {
			// 如果更长，则用定高
			finalHeight = designSize.height;
			finalWidth = finalHeight * frameWidth / frameHeight;
		} else {
			/*!#zh: 是否优先将设计分辨率宽度撑满视图宽度。 */
			//如果更短，则用定宽
			finalWidth = designSize.width;
			finalHeight = frameHeight / frameWidth * finalWidth;
			centerScale = finalHeight/designSize.height;
		}
		this._fixedCenterView(centerScale);
		view.setDesignResolutionSize(finalWidth, finalHeight, ResolutionPolicy.FIXED_WIDTH);
		canvas.getComponent(UITransform).width = finalWidth;
		canvas.getComponent(UITransform).height = finalHeight;
		canvas.node.emit('resize');
		return size(finalWidth, finalHeight);
	};

	private _fixedBg(canvasSize: Size) {
		const bg = this.node.getChildByName("bg");
		if (!bg) {
			return;
		}
		let scale: number = 1;
		const bgSize = bg.getComponent(UITransform).contentSize;
		if (bgSize.width >= canvasSize.width && bgSize.height >= canvasSize.height) {
			scale = 1;
		} else if (bgSize.width < canvasSize.width) {
			scale = canvasSize.width / bgSize.width;
		} else if (bgSize.height < canvasSize.height) {
			scale = canvasSize.height / bgSize.height;
		} else {
			const scaleX = canvasSize.width / bgSize.width;
			const scaleY = canvasSize.height / bgSize.height;
			scale = scaleX > scaleY ? scaleX : scaleY;
		}
		bg.scale = v3(scale, scale, 1);
	}

	private _fixedCenterView(scale: number) {
		if (!this.m_centerView) {
			return;
		}
		this.m_centerView.scale = v3(scale, scale, 1);
	}
}

